// pages/refund/refund.ts
import animationHelper from '../../utils/animation';
import api from '../../utils/api';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showDialog: false as boolean,
    reasonList: [
      {
        id: 0,
        text: '不想要了',
        checked: false,
      },
      {
        id: 1,
        text: '地址信息填写错误',
        checked: false
      },
      {
        id: 2,
        text: '商品错选/多选',
        checked: false
      },
      {
        id: 3,
        text: '商品降价',
        checked: false
      },
      {
        id: 4,
        text: '没用/少用/错用优惠',
        checked: false
      },
    ] as Reason[],
    animation: null as WechatMiniprogram.Animation | null,
    animationData: {} as any,
    chooseReason: '' as string,
    orderInfo: {} as OrderModel,//订单信息
    remark:'' as string,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options:any) {
    try {
      const orderInfo = JSON.parse(options.data)
      this.setData({
        orderInfo
      })
    } catch (error) {
      console.error("JSON.parse has question")
    }

  },
  // 提交申请
  submit(): void {
    const { chooseReason, remark, orderInfo } = this.data
    if (!chooseReason) {
      wx.showToast({
        title: "请选择原因",
        icon: "error"
      })
      return
    }
    const params:OrderApplyRefundParam = {
      id: orderInfo.id,
      remark,
      reason:chooseReason
    }
    api.order.applyRefund(params).then(res => {
      wx.navigateBack()
      if (res.data.code == "000") {
      } else {
        wx.showToast({
          title: res.data.message as string,
          icon: "error"
        })
      }
    })
  },
  // 关闭弹窗
  closeTips(): void {
    const animation = this.data.animation;
    animationHelper.closeAnimation(animation, 589, this.setData.bind(this), 'showDialog');
  },
  // 退款原因选择
  changeStatus(e: any): void {
    const { reasonList } = this.data
    const index = reasonList.findIndex(item => {
      return item.id === e.currentTarget.dataset.id
    })
    reasonList.map(item => {
      item.checked = false
    })
    reasonList[index].checked = true
    this.setData({
      reasonList
    })
  },
  // 点击取消订单
  showDialog(): void {
    const animation = animationHelper.createAnimation(500, 'ease');
    this.setData({
      animation: animation
    });
    animationHelper.showAnimation(animation, 589, this.setData.bind(this), 'showDialog');
  },
  // 点击确认
  chooseRefundReason(): void {
    const { reasonList } = this.data
    // 判断原因是否选择
    const index = reasonList.findIndex(item => {
      return item.checked
    })
    if (index < 0) {
      wx.showToast({
        title: "请选择原因",
        icon: "error"
      })
      return
    }
    this.setData({
      chooseReason: reasonList[index].text
    })
    this.closeTips();
  },
  // 处理输入
  handleInput(e:any): void {
    this.setData({
      remark: e.detail.value
    })
  },

})